//
//  MenuTool.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/5.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "MenuToolMarket.h"

@interface MenuToolMarket ()
@property(nonatomic, strong)NSMutableArray *aticleArray;
@property(nonatomic, strong)NSMutableArray *selectedArray;
@property(nonatomic, strong)NSMutableArray *allArray;
@end

@implementation MenuToolMarket
singleton_implementation(MenuToolMarket)
-(NSMutableArray *)allArray{
    if (!_allArray) {
        _allArray = [NSMutableArray array];
        AticleMenuMarket * menu1 = [[AticleMenuMarket alloc]init];
        menu1.resident = YES;
        menu1.menuId = -1;
        menu1.selected = YES;
        menu1.isEditing = NO;
        menu1.Text = @"自选";
        
        AticleMenuMarket * menu2 = [[AticleMenuMarket alloc]init];
        menu2.resident = YES;
        menu2.menuId = 0;
        menu2.selected = YES;
        menu1.isEditing = NO;
        menu2.Text = @"市值";
//        [_allArray addObject:menu1];
//        [_allArray addObject:menu2];
    }
    return _allArray;
}
+(void)load{
    
    AticleMenuMarket * menu1 = [[AticleMenuMarket alloc]init];
    menu1.resident = YES;
    menu1.menuId = -1;
    menu1.selected = YES;
    menu1.isEditing = NO;
    menu1.Text = @"自选";
    AticleMenuMarket * menu2 = [[AticleMenuMarket alloc]init];
    menu2.resident = YES;
    menu2.menuId = 0;
    menu2.selected = YES;
    menu1.isEditing = NO;
    menu2.Text = @"市值";
    [menu1 saveOrUpdate];
    [menu2 saveOrUpdate];
    
    NSMutableArray * newArray = [NSMutableArray array];
    NSArray * locationArray = [AticleMenuMarket findAll];
    
//    [FBNetworkTool get:@"quotationapi/com.GetQuotationMenu" parameters:@{} showHUD:NO success:^(XMHttpResponseModel *responseModel) {
//        if (responseModel.code == 200) {
//            NSArray * array = [AticleMenuMarket mj_objectArrayWithKeyValuesArray:responseModel.data];
//            ///判断用户有没有在本地选择过
//            if ([XMUserDefaults boolForKey:@"userSelectedMenuMarket"]) {
//                ///选择过了，拿到的数据跟本地数据对比。有新增的保存本地
//                for (AticleMenuMarket * menu in array) {
//                    for (AticleMenuMarket * location in locationArray) {
//                        if (menu.menuId != location.menuId && ![menu.Text isEqualToString:location.Text]) {
//                            [newArray addObject:menu];
//                        }
//                    }
//                }
//
//                if (newArray.count > 0) {
//                    [AticleMenuMarket saveArray:newArray];
//                }
//            }else{//用户没有编辑过，本地没有保存。
//                if (array.count > 0) {
//                    NSInteger number = (array.count >= 6)?6:array.count;
//                    for (NSInteger i = 0; i < number; i++) {
//                        AticleMenuMarket * menu = array[i];
//                        menu.selected = YES;
////                        [newArray addObject:menu];
//                    }
//                    [AticleMenuMarket saveArray:array];
//                }
//            }
//        }
//    } failure:^(NSError *error) {
//    }];
}

-(NSArray *)localSelectedMenus{
    NSArray * array = [AticleMenuMarket findWhere:@[@"selected",@"=",@(YES)]];
    [self.allArray addObjectsFromArray:array];
    return self.allArray;
}

+(NSArray *)userSelectedMenu{
    return [AticleMenuMarket findWhere:@[@"selected",@"=",@(YES)]];
}
+(NSArray *)userUnselectedMenu{
    return [AticleMenuMarket findWhere:@[@"selected",@"=",@(NO)]];
}


@end
